package javolution.context;

import javax.realtime.MemoryArea;
import javolution.util.FastMap;
import javolution.util.FastTable;

/* loaded from: classes2.dex */
public final class ImmortalContext extends AllocatorContext {
    private static final Class CLASS = new ImmortalContext().getClass();
    private static final ThreadLocal FACTORY_TO_ALLOCATOR = new ThreadLocal() { // from class: javolution.context.ImmortalContext.1
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new FastMap();
        }
    };
    private static final ThreadLocal ACTIVE_ALLOCATORS = new ThreadLocal() { // from class: javolution.context.ImmortalContext.2
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new FastTable();
        }
    };

    /* loaded from: classes2.dex */
    private static final class ImmortalAllocator extends Allocator {
        private static final MemoryArea IMMORTAL = MemoryArea.getMemoryArea("");
        private Object _allocated;
        private final ObjectFactory _factory;
        private final Runnable _allocate = new Runnable() { // from class: javolution.context.ImmortalContext.ImmortalAllocator.1
            @Override // java.lang.Runnable
            public void run() {
                ImmortalAllocator immortalAllocator = ImmortalAllocator.this;
                immortalAllocator._allocated = immortalAllocator._factory.create();
            }
        };
        private final Runnable _resize = new Runnable() { // from class: javolution.context.ImmortalContext.ImmortalAllocator.2
            @Override // java.lang.Runnable
            public void run() {
                ImmortalAllocator.this.resize();
            }
        };

        public ImmortalAllocator(ObjectFactory objectFactory) {
            this._factory = objectFactory;
        }

        @Override // javolution.context.Allocator
        protected Object allocate() {
            IMMORTAL.executeInArea(this._allocate);
            return this._allocated;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        /* JADX WARN: Multi-variable type inference failed */
        @Override // javolution.context.Allocator
        public void recycle(Object obj) {
            if (this._factory.doCleanup()) {
                this._factory.cleanup(obj);
            }
            if (this.queueSize >= this.queue.length) {
                IMMORTAL.executeInArea(this._resize);
            }
            T[] tArr = this.queue;
            int i = this.queueSize;
            this.queueSize = i + 1;
            tArr[i] = obj;
        }

        public String toString() {
            return "Immortal allocator for " + this._factory.getClass();
        }
    }

    static {
        ObjectFactory.setInstance(new ObjectFactory() { // from class: javolution.context.ImmortalContext.3
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new ImmortalContext();
            }
        }, CLASS);
    }

    private ImmortalContext() {
    }

    public static ImmortalContext enter() {
        return (ImmortalContext) Context.enter(CLASS);
    }

    public static ImmortalContext exit() {
        return (ImmortalContext) Context.exit();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // javolution.context.AllocatorContext
    public void deactivate() {
        FastTable fastTable = (FastTable) ACTIVE_ALLOCATORS.get();
        int size = fastTable.size();
        for (int i = 0; i < size; i++) {
            ((Allocator) fastTable.get(i)).user = null;
        }
        fastTable.clear();
    }

    @Override // javolution.context.Context
    protected void enterAction() {
        getOuter().getAllocatorContext().deactivate();
    }

    @Override // javolution.context.Context
    protected void exitAction() {
        deactivate();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // javolution.context.AllocatorContext
    public Allocator getAllocator(ObjectFactory objectFactory) {
        FastMap fastMap = (FastMap) FACTORY_TO_ALLOCATOR.get();
        ImmortalAllocator immortalAllocator = (ImmortalAllocator) fastMap.get(objectFactory);
        if (immortalAllocator == null) {
            immortalAllocator = new ImmortalAllocator(objectFactory);
            fastMap.put(objectFactory, immortalAllocator);
        }
        if (immortalAllocator.user == null) {
            immortalAllocator.user = Thread.currentThread();
            ((FastTable) ACTIVE_ALLOCATORS.get()).add(immortalAllocator);
        }
        return immortalAllocator;
    }
}
